//
//  MKChainedClass.m
//  ObjectCChained
//
//  Created by sny on 2018/5/21.
//  Copyright © 2018年 HuangYangYang. All rights reserved.
//

#import "MKChainedClass.h"
#import "MKChainedClassError.h"

@interface MKChainedClass()

@end

@implementation MKChainedClass

#pragma mark - ChainedMethond

- (MKChainedClass *(^)(NSArray*))initParamsWithArray {
    return ^MKChainedClass *(NSArray *params) {
        [self setAllPropertiesWithArray:params];
        return self;
    };
}

- (MKChainedClass *(^)(NSDictionary *))intParamsWithDictionary {
    return ^MKChainedClass *(NSDictionary *params) {
        [self setAllPropertiesWithDictionary:params];
        return self;
    };
}

#pragma mark - setValueMethond

- (void)setAllPropertiesWithArray:(NSArray *)params{
    
    u_int count;//属性的数量
    
    //获取属性名称
    objc_property_t *properties  =class_copyPropertyList([self class], &count);
    
    //是否允许空值的存在
    self.allowEmpty = [self propertyAllowEmpty];
    
    //当允许空值存在的时候
    if (self.allowEmpty == YES) {
        
        //当传入的值小于属性数量的时候
        if (params.count < count) {
            
            [self setValueWithCount:params.count WithParams:params withProperties:properties];
        
        }

        //当传入的值大于等于属性数量的时候
        if (params.count >= count) {
            
            [self setValueWithCount:params.count WithParams:params withProperties:properties];
            
        }
    }
    
    //当不允许空值存在的时候
    if (self.allowEmpty == NO) {
        
        //如果传入的值和属性数量不一致的时候
        if (params.count != count) {
            
            return [MKChainedClassError showErrorWithMKChainedClassErrorType:MKChainedClassErrorCrossing];
            
        }else{
            //当传入一致的时候
            [self setValueWithCount:params.count WithParams:params withProperties:properties];
        }
    }
    
    free(properties);
}

- (void)setAllPropertiesWithDictionary:(NSDictionary *)params {
    u_int count;//属性的数量
    
    //获取属性名称
    objc_property_t *properties  = class_copyPropertyList([self class], &count);
    
    //是否允许空值的存在
    self.allowEmpty = [self propertyAllowEmpty];
    
    //当允许空值存在的时候
    if (self.allowEmpty  == YES) {
        
        //当传入的值小于属性数量的时候
        if (params.count < count) {
            [self setValueWithCount:params.count WithDictionaryParams:params withProperties:properties];
        }
        
        //当传入的值大于等于属性数量的时候
        if (params.count >= count) {
            [self setValueWithCount:count WithDictionaryParams:params withProperties:properties];
        }
    }
    
    //当不允许空值存在的时候
    if (self.allowEmpty  == NO) {
        
        //如果传入的值和属性数量不一致的时候
        if (params.count != count) {
            
            return [MKChainedClassError showErrorWithMKChainedClassErrorType:MKChainedClassErrorInvalid];
            
        }else{
            //当传入一致的时候
            [self setValueWithCount:count WithDictionaryParams:params withProperties:properties];
            
        }
    }
    free(properties);
}

- (BOOL)propertyAllowEmpty {
    return YES;
}

@end
